package com.atguigu.juc.locks;

/**
 * 在一个Synchronized修饰的方法或代码块的内部调用本类的其他Synchronized修饰的方法或代码块时，是永远可以得到锁的
 *
 * @author yfstart
 * @create 2022-06-28 21:17
 */
public class ReEntryLockDemo {
  public static void main(String[] args) {
    // reEntryMethod1();
    ReEntryLockDemo reEntryLockDemo = new ReEntryLockDemo();

    new Thread(() -> {
        reEntryLockDemo.m1();
    },"t2").start();
  }

    private static void reEntryMethod1() {
        final Object object = new Object();
        new Thread(()->{
            synchronized (object){
                System.out.println(Thread.currentThread().getName()+"\t ---外层调用");
                synchronized (object){
                    System.out.println(Thread.currentThread().getName() + "\t ---中层调用");
                    synchronized (object){
                        System.out.println(Thread.currentThread().getName() + "\t ---内层调用");
                    }
                }
            }
        },"t1").start();
    }

  public synchronized void m1() {
    System.out.println(Thread.currentThread().getName() + "\t ---m1 come in");
    m2();
  }

  public synchronized void m2() {
    System.out.println(Thread.currentThread().getName() + "\t ---m2 come in");
    m3();
  }

  public synchronized void m3() {
    System.out.println(Thread.currentThread().getName() + "\t ---m3 come in");
  }
}
